package org.openems.otdr;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.openems.meta.MissionModelMeta;
import org.openems.model.MissionModel;
import org.slim3.datastore.Datastore;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@Path("/otdrMission")
public class MissionResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public OtdrMission get() {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        if (user != null) {
            String email = user.getEmail();
            MissionModelMeta e = MissionModelMeta.get();
            List<MissionModel> models = Datastore.query(e).filter(e.assignee.equal(email)).asList();
            if (models.size() == 0) {
                return null;
            }
            MissionModel model = models.get(0);
            
            OtdrMission ret = new OtdrMission();
            ret.director = model.getDirector();
            ret.assignee = model.getAssignee();
            return ret;
        }
        return null;
    }
}
